<?php

namespace App\Modules\Apicrzs\Services\Water;


use App\Modules\Apicrzs\Entities\ApparatusModel;
use App\Modules\Apicrzs\Entities\UserScoreModel;
use App\Modules\Apicrzs\Services\BaseServices;
use App\Traits\CryptAES;
use Illuminate\Support\Facades\DB;

class DeviceService extends BaseServices
{
    public function __construct(ApparatusModel $model)
    {
        $token = \request()->header('token');
        $user_info = CryptAES::decryptToken($token, 'user_grzq');
        $this->user_id = $user_info['data']['user_id'] ?? "";
        $this->model = $model;
        $this->userScoreModel = new UserScoreModel();
    }

    public function getLists($params)
    {
        #经度
        $lng = $params["lng"] ?? "";
        #纬度
        $lat = $params["lat"] ?? "";
        #获取指定范围的数据单位km
        $distance_sea = 100;
        #数据模型
        $model = $this->model->where(['status' => 1]);
        if (!empty($lng)) {
            $distance = "ACOS(SIN(( $lat * 3.1415) / 180 ) *SIN((lat * 3.1415) / 180 ) +COS(( $lat* 3.1415) / 180 ) * COS((lat * 3.1415) / 180 ) *COS(( $lng* 3.1415) / 180 - (lng * 3.1415) / 180 ) ) * 6380";
            $model->addSelect(DB::raw($distance . ' as distance'))->orderBy('distance', 'ASC');
            #控制范围
            $model->whereRaw($distance . "<" . $distance_sea);
        }else{
            $model->orderBy($this->model->getKeyName(), 'DESC');
        }
        $model->select(['id', 'title', 'address', 'lng', 'lat']);
        $lists = $model->paginate($this->getLimit($params['limit'] ?? 10));
        $lists_data=[];
        foreach ($lists->items() as $item) {
            $lists_data[] = [
                'id' => $item['id'],
                'title' => $item['title'],
                'code' => $item['code'],
                'address' => $item['address'],
                'lng' => $item['lng'],
                'lat' => $item['lat'],
                'distance' => $item['distance']??0,
            ];
        }

        return [
            'current_page' => $lists->currentPage(),//当前
            'per_page' => $lists->perPage(),//每页 10 条
            'count_page' => $lists->lastPage(),//
            'total' => $lists->total(), // 总数
            'data' => $lists_data,
        ];
    }

    /**
     * @param $params type 1 卡券 ，2 套餐
     * @return mixed
     */
    public function getListss($params)
    {

        $type = $params['type'] ?? 1;
        $lists = $this->model
            ->orderBy($this->model->getKeyName(), 'DESC')
            ->paginate($this->getLimit($params['limit'] ?? 10));
        $lists_data = [];
        foreach ($lists->items() as $item) {
            $lists_data[] = [
                'id' => $item['id'],
                'title' => $item['title'],
                'code' => $item['code'],
                'address' => $item['address'],
                'lng' => $item['lng'],
                'lat' => $item['lat'],
            ];
        }

        return [
            'current_page' => $lists->currentPage(),//当前
            'per_page' => $lists->perPage(),//每页 10 条
            'count_page' => $lists->lastPage(),//
            'total' => $lists->total(), // 总数
            'data' => $lists_data,
        ];
    }
}
